﻿using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml;
using BlogsAPI.Sites;
using RssToolkit.Rss;

namespace BlogsAPI.Rdf
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using RssToolkit.Xml;

    [TestClass]
    public class RdfUnitTest
    {
        /// <summary>
        /// Parsing FOAF data from Internet
        /// </summary>
        [TestMethod]
        public void LoadFoaf()
        {
            Uri[] FOAFs = new[] { 
                new Uri("http://www.liveinternet.ru/users/flashr/foaf/"), 
                new Uri("http://community.livejournal.com/ktosomnoi/data/foaf"),
                new Uri("http://dolboeb.livejournal.com/data/foaf"),
                new Uri("http://flashr.ya.ru/foaf.xml"),
                new Uri("http://clubs.ya.ru/ya/foaf.xml"), 
                new Uri("http://eikeon.com/foaf.rdf"), 
                new Uri("http://madskristensen.net/foaf.axd"),
                new Uri("http://topbot.ru/foaf"), 
            };

            foreach (Uri linkto in FOAFs)
            {
                RdfDocument rDoc = RdfDocument.Load(linkto);
                Assert.AreNotEqual(null, rDoc.Who);
            //    String foafFile = rDoc.ToXml(DocumentType.Rdf);
            //    Assert.AreEqual(false, String.IsNullOrEmpty(foafFile));
            }
        }

        /// <summary>
        /// Parsing RDF at http://www.livejournal.com/stats/latest-rss.bml
        /// </summary>
        [TestMethod]
        public void LoadLivejournalLatest()
        {
            //RdfDocument rDoc = RdfDocument.Load(new XmlTextReader(File.Open("c:\\Temp\\latest.txt",FileMode.Open)));
            RssDocument xDoc = RssDocument.Load(new XmlTextReader(File.Open("c:\\Temp\\latest.txt", FileMode.Open)));
            Assert.AreNotEqual(null, xDoc.Channel);
        }

        /// <summary>
        /// Making FOAF file
        /// </summary>
        [TestMethod]
        public void PublishFoaf()
        {
            RdfDocument rdf = new RdfDocument
                  {
                      Version = "0.9",
                      Lang = "ru",
                      Who = new FoafPerson
                        {
                            Pages = new[]
                            {
                                     new RdfElement
                                     {
                                         Resource = "http://ya.topbot.ru/feedback.aspx",
                                         Description = "Flashr's contact information",
                                         Date = DateTime.Now.ToString("yyyy-MM-dd"),
                                     },
                            },
                            Language = "ru-RU",
                            Name = "Flashr",
                            Nick = "Topbot",
                            About = "Робот блогосферы",
                            BirthDay = "1985-10-11",
                            Bio = ".Net Developer",
                            City = new RdfElement
                                       {
                                           Title = "Москва",
                                           Resource = "http://maps.google.com/maps?f=q&source=s_q&hl=ru&geocode=&q=%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0&sll=37.0625,-95.677068&sspn=27.976484,63.544922&ie=UTF8&hq=&hnear=%D0%A0%D0%BE%D1%81%D1%81%D0%B8%D1%8F,+%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0&t=h&z=9"
                                       },
                            //Gender = FoafGender.male,
                            Weblog = new WeblogExt
                                         {
                                             Title = "My Blog",
                                             Resource = "http://ya.topbot.ru"
                                         },
                            Activities = new []
                             {
                                 new BlogActivityClass{
                                     data = new PostsActivity
                                     {
                                         RSS =
                                             "http://feeds.feedburner.com/Topbot",
                                         Posted = 666
                                     }},
                                 new BlogActivityClass{
                                     data = new CommentsActivity
                                     {
                                         Received = 11
                                     }
                                 }
                             },
                             Knows = new []{
                                 new FoafKnow
                                 {
                                     Person = new FoafPerson
                                      {
                                          
                                          Nick = "idiot1",
                                          Name = "Someone",
                                          Weblog = new WeblogExt
                                          {
                                               Resource = "http://idiot1.livejournal.com"
                                          }
                                      }
                                 },
                                 new FoafKnow
                                 {
                                     Person = new FoafGroup
                                      {
                                          Name = "Клуб алкоголиков",
                                          Nick = "alco_club",
                                          Weblog = new WeblogExt
                                           {
                                            Resource   = "http://community.livejournal.com/alco_club"
                                           }

                                      }
                                 }
                        }
                    }
              };
            //
            String foafFile = rdf.ToXml(DocumentType.Rdf);
            Assert.AreEqual(false,String.IsNullOrEmpty(foafFile));
        }

        /// <summary>
        /// Check if Google RSS works
        /// </summary>
        [TestMethod]
        public void GetGoogleRss()
        {
            var gp = new GooglePlus();
            //RssDocument rss = gp.GetRss("+ERAEVAEvelina");
            RssDocument rss = gp.GetRss("103402709177915406298");
            Assert.IsTrue(rss.Channel.Items.Count > 0);
        }

        /// <summary>
        /// Check if Instagram RSS works
        /// </summary>
        [TestMethod]
        public void GetInstagramRss()
        {
            var ig = new Instagram();
            RssDocument rss = ig.GetRss("darjaskl");
            if(Common.IsRussian(rss))
            {
                Debug.WriteLine("is russian =((");
            }
            //RssDocument rss = ig.GetRss("damedvedev");
            Assert.IsTrue(rss.Channel.Items.Count > 0);
        }

        /// <summary>
        /// Check if Facebook RSS works
        /// </summary>
        [TestMethod]
        public void GetFacebookRss()
        {
            var fb = new BlogsAPI.Sites.Facebook();
            RssDocument rss = RssDocument.Load(new Uri(fb.GetRssLink("309148850654")));
            //RssDocument rss = RssDocument.Load(File.ReadAllText("C:\\Temp\\1.txt"));
            Assert.IsTrue(rss.Channel.Items.Count > 0);
        }

    }
}
